package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.Backtracking;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tzp
 * @since 2020/10/16
 */
public class LC78_1 implements Backtracking {
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        if (nums == null || nums.length == 0) return Collections.emptyList();
        List<Integer> path = new ArrayList<>();
        for (int i = 0; i <= nums.length; i++) {
            backtracking(nums, i, path);
        }
        return result;
    }

    public void backtracking(int[] nums, int remainLength, List<Integer> path) {
        if (remainLength == 0) {
            result.add(path.stream().map(x -> nums[x]).collect(Collectors.toList()));
        } else {
            int x = path.size() > 0 ? path.get(path.size() - 1) + 1 : 0;
            for (int i = x; i < nums.length; i++) {
                path.add(new Integer(i));//mark used
                backtracking(nums, remainLength - 1, path);
                path.remove(new Integer(i));//mark unused
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new LC78_1().subsets(new int[]{1, 2, 3}));
    }
}
